﻿// <copyright file="MovieOrderConverter.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2010-09-13</date>
// <summary>MovieOrderConverter value converter</summary>

namespace NYTMovieReviews.ValueConverters
{
    using System;
    using System.Windows;
    using System.Windows.Data;
    using NYTimes.Data;

    /// <summary>
    /// Value converter for Movie orders
    /// </summary>
    public class MovieOrderConverter : IValueConverter
    {
        /// <summary>
        /// Convert from a MovieReviewOrder
        /// </summary>
        /// <param name="value">a MovieReviewOrder</param>
        /// <param name="targetType">the target type</param>
        /// <param name="parameter">the parameter</param>
        /// <param name="culture">the culture</param>
        /// <returns>true or false</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null && parameter == null)
            {
                return true;
            }

            if (value == null)
            {
                return false;
            }

            var order = (MovieReviewOrder)value;

            if (parameter == null)
            {
                return false;
            }

            return order.ToString() == parameter.ToString();
        }

        /// <summary>
        /// Convert to a MovieReviewOrder
        /// </summary>
        /// <param name="value">a boolean value</param>
        /// <param name="targetType">the target type</param>
        /// <param name="parameter">the parameter</param>
        /// <param name="culture">the culture</param>
        /// <returns>the movie review order</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return DependencyProperty.UnsetValue;
            }

            if (parameter == null)
            {
                return null;
            }

            var movieReviewOrder = (MovieReviewOrder)Enum.Parse(typeof(MovieReviewOrder), parameter.ToString(), true);

            var set = (bool)value;

            if (set)
            {
                return movieReviewOrder;
            }

            return DependencyProperty.UnsetValue; 
        }
    }
}
